package in.muton.edushare.pages.user.panels;

import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.pages.user.CreateGroup;
import in.muton.edushare.pages.user.EditProfilePage;
import in.muton.edushare.pages.user.UserAssignment;
import in.muton.edushare.pages.user.UserBase;
import in.muton.edushare.pages.user.UserCalendar;
import in.muton.edushare.pages.user.UserCourses;
import in.muton.edushare.pages.user.UserDept;
import in.muton.edushare.pages.user.UserFolders;
import in.muton.edushare.pages.user.UserGrades;
import in.muton.edushare.pages.user.UserPage;
import in.muton.edushare.pages.user.UserPolls;
import in.muton.edushare.pages.user.UserSettings;
import in.muton.edushare.pages.user.UserUniv;
import in.muton.edushare.processing.objects.UserDataHandler;
import in.muton.edushare.url.EntityPageParams;
import in.muton.edushare.wicket.models.CayenneList;

import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Enrolments;
import com.edushare.common.vo.Users;

public class MyLeftMenuPanel extends EdusharePanel {

	private static final long serialVersionUID = 302300103272339794L;
	private UserBase parentPage;

	protected static String[] menuItems = { "UserUpdates", "UserAssignments", "UserFiles",
		"UserCalendar", "UserProfile", "UserUniv", "UserDept",
		"UserCourses", "UserSettings" , "UserPolls", "UserGroups", "CourseGrades"};

	public MyLeftMenuPanel(String id, UserBase page) {
		super(id, page);
		this.parentPage = page;
		setMenuItems();
	}

	public void setMenuItems() {
		String highlight = this.getActivePage();
		boolean sameUser = getLoggedInUser() == getModel().getObject();
		for (String s : menuItems) {
			WebMarkupContainer wmc;
			add(wmc = new WebMarkupContainer(s));
			EntityPageParams.getParams(getModel(), s);
			if (highlight.compareTo(s) == 0) {
				wmc.add(new AttributeAppender("class", new Model<String>(
						"es-menuItemSelected"), " "));
			}
			if (s.equals("UserUpdates")) {
				wmc.add(new BookmarkablePageLink<Void>("link", UserPage.class,
						EntityPageParams.getParams(getModel())));
			} else if (s.equals("UserAssignments")) {
				wmc.add(new BookmarkablePageLink<Void>("link", UserAssignment.class,
						EntityPageParams.getParams(getModel())));
				wmc.setVisible(sameUser);
			} else if (s.equals("UserFiles")) {
				wmc.add(new BookmarkablePageLink<Void>("link", UserFolders.class,
						EntityPageParams.getParams(getModel())));
			} else if (s.equals("UserCalendar")) {
				wmc.add(new BookmarkablePageLink<Void>("link", UserCalendar.class,
						EntityPageParams.getParams(getModel())));
			} else if (s.equals("UserProfile")) {
				wmc.add(new BookmarkablePageLink<Void>("link", EditProfilePage.class,
						EntityPageParams.getParams(getModel())));
				wmc.setVisible(sameUser);
			} else if (s.equals("UserUniv")) {
				wmc.add(new BookmarkablePageLink<Void>("link", UserUniv.class,
						EntityPageParams.getParams(getModel())));
			} else if (s.equals("UserDept")) {
				wmc.add(new BookmarkablePageLink<Void>("link", UserDept.class,
						EntityPageParams.getParams(getModel())));
			} else if (s.equals("UserCourses")) {
				wmc.add(new BookmarkablePageLink<Void>("link",
						UserCourses.class, EntityPageParams
								.getParams(getModel())));
			} else if(s.equals("UserSettings")){
				wmc.add(new BookmarkablePageLink<Void>("link",
						UserSettings.class, EntityPageParams
								.getParams(getModel())));
				wmc.setVisible(sameUser);
			} else if(s.equals("UserPolls")){
				wmc.add(new BookmarkablePageLink<Void>("link",
						UserPolls.class, EntityPageParams
								.getParams(getModel())));
				wmc.setVisible(sameUser);
			}else if(s.equals("UserGroups")){
				wmc.add(
						new BookmarkablePageLink<Void>(
								"link", 
								CreateGroup.class, 
								EntityPageParams.getParams(getModel()))
						);
			}else if(s.equals("CourseGrades")){
				wmc.add(new BookmarkablePageLink<Void>("link",
						UserGrades.class, EntityPageParams.getParams(getModel())));
				UserDataHandler ud = new UserDataHandler(getModel());
				CayenneList<CourseInstances> l = ud.getCourseInstByRole(Enrolments.STUDENT);
				l.add(ud.getCourseInstByRole(Enrolments.OBSERVER));
				if(l.getObject().size()==0)
					wmc.setVisible(false);
			}
		}
	}

	private String getActivePage() {
		return parentPage.getActivePage();
	}

	private IModel<Users> getModel() {
		return parentPage.getModel();
	}
	
	
}
